package eu.telecombretagne.rsm.gui.frames.panels;

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JTextField;

import eu.telecombretagne.rsm.base.event.manager.EventParamsManager;
import eu.telecombretagne.rsm.gui.NameConsts;

public class GlobalSettingsPanel extends SettingsPanel {
	
	private static final long serialVersionUID = -6606712299565585753L;
	
	private JLabel labSettings;
	private JTextField fldGlobalProba;
	private JTextField fldGlobalDelay;
	private EffectsPanel effectsPanel;
	
	/**
	 * The constructor of the GlobalSettingsPanel.
	 */
	public GlobalSettingsPanel(){
		setLayout(new GridLayout(1, 3));
		setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), NameConsts.SIMPLIFIEDSETTINGS));
		
		labSettings = new JLabel(NameConsts.GLOBALVALUES);
		
		fldGlobalProba = new JTextField(NameConsts.DEFAULTPROBABILITY);
		fldGlobalProba.setToolTipText(NameConsts.PROBABILITY);
		
		fldGlobalDelay = new JTextField(NameConsts.DEFAULTDELAY);
		fldGlobalDelay.setToolTipText(NameConsts.DELAYINMILLISECONDS);
		
		add(labSettings);
		add(createSubPanel(fldGlobalProba, fldGlobalDelay));
		add(Box.createGlue());
	}

	@Override
	public void readAllParams() {
		EventParamsManager.setUseSimplifiedValues(true);
		super.readAllParams();
	}

	@Override
	protected void readProbabilities() {
		setEventProbability(fldGlobalProba);
	}

	@Override
	protected void readDelays() {
		setEventDelay(fldGlobalDelay);
	}
	
	@Override
	protected void readEffects() {
		setEventEffects(effectsPanel);
	}

	@Override
	protected Long setEventDelay(JTextField pTextField) {
		Long delay = 0L;
		
		if(getTextFromFieldAndParseToDouble(pTextField) != null){
			delay = getTextFromFieldAndParseToLong(pTextField);
		}
		
		if(pTextField == fldGlobalDelay){
			delay = EventParamsManager.setGlobalEventDelay(delay);
		}
		
		pTextField.setText(String.valueOf(delay));
		
		return delay;
	}

	@Override
	protected Double setEventProbability(JTextField pTextField) {
		Double probability = 1.0;
		
		if(getTextFromFieldAndParseToDouble(pTextField) != null){
			probability = getTextFromFieldAndParseToDouble(pTextField);
		}
		
		if(pTextField == fldGlobalProba){
			probability = EventParamsManager.setGlobalEventProbability(probability);
		}
		
		pTextField.setText(String.valueOf(probability));
		
		return probability;
	}
	
	@Override
	protected void setEventEffects(EffectsPanel pEffectsPanel) {
//		Boolean effects = ! pEffectsPanel.isNoneEnabled();
//		Boolean duplication = pEffectsPanel.isDuplicationEnabled();
//		
//		if(pEffectsPanel == effectsPanel){
//			EventParamsManager.setGlobalEventEffects(effects, duplication);
//		}
	}
}